package main

import (
	"filetools/v2/pkg"
	"fmt"
	"net"
	"sync"

	"github.com/rainyt/golib/util"
)

var wait sync.WaitGroup

func main() {
	socket, err := net.Dial("tcp", "127.0.0.1:3333")
	if err != nil {
		panic(err)
	}
	go onClientRead(socket)
	// 写入数据
	uploadData := &pkg.FileData{}
	uploadData.Name = "test.com"
	uploadData.Desc = "初次提交的内容"
	// 模拟一个文件
	file := &pkg.File{
		Path: "/test.txt",
		Bytes: &util.Bytes{
			Data: []byte("测试内容"),
		},
	}
	uploadData.WriteFile(file)
	socket.Write(uploadData.GetData())
	wait.Add(1)
	wait.Wait()
}

func onClientRead(client net.Conn) {
	for {
		var data [128]byte
		size, err := client.Read(data[:])
		if size > 0 {
			content := string(data[0:size])
			fmt.Println("提交结果：", content)
		}
		if err != nil {
			wait.Done()
			break
		}
	}
}
